package com.wt.controller.controller.user;

import com.wt.controller.model.base.ErrorModel;
import com.wt.controller.model.base.SuccessModel;
import com.wt.controller.model.user.AddUserReq;
import com.wt.dao.demo.model.User;
import com.wt.dao.demo.model.UserListModel;
import com.wt.dao.demo.model.UserLogin;
import com.wt.service.exception.WTException;
import com.wt.service.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Controller
@RequestMapping("user")
public class UserController {

    @Autowired
    private UserService userService;

    private final static Logger LOGGER = Logger.getLogger(UserController.class);

    @RequestMapping("addUser")
    @ResponseBody
    public Object addUser(AddUserReq addUserReq, HttpServletRequest request)
    {
        if(null != addUserReq)
        {
            LOGGER.info(addUserReq.toString());

            //参数校验
            if(StringUtils.isNotBlank(check(addUserReq)))
            {
                LOGGER.error(check(addUserReq));
                return new ErrorModel(check(addUserReq));
            }

            //参数转换
            User user = convertUser(addUserReq);
            UserLogin userLogin = convertUserLogin(addUserReq);

            try
            {
                //调用service
                userService.addUser(user,userLogin);
            }
            catch (WTException e)
            {
                LOGGER.error(e.getMessage());
                return new ErrorModel(e.getMessage());
            }
            catch (Exception e)
            {
                LOGGER.error(e);
                return new ErrorModel("注册失败，请联系管理员！");
            }
        }
        else
        {
            return new ErrorModel("请求为空");
        }
        return new SuccessModel();
    }

    @RequestMapping("getUserInfo")
    @ResponseBody
    public Object getUserInfo(String userId, HttpServletRequest request)
    {
        if(StringUtils.isBlank(userId))
        {
            LOGGER.error("用户id为空！");
            return new ErrorModel();
        }
        User resultUser = null;
        try {
            resultUser = userService.selectByUserId(Integer.valueOf(userId));
        }
        catch (WTException e)
        {
            LOGGER.error(e);
            return new ErrorModel(e);
        }
        catch (Exception e)
        {
            LOGGER.error(e);
            return new ErrorModel("获取用户信息失败，请联系管理员！");
        }

        return new SuccessModel(resultUser);
    }

    @RequestMapping("getUserList")
    @ResponseBody
    public Object getUserList( HttpServletRequest request)
    {
        List<UserListModel> userList = null;
        try {
            userList = userService.getUserList();
        }
        catch (WTException e)
        {
            LOGGER.error(e);
            return new ErrorModel(e);
        }
        catch (Exception e)
        {
            LOGGER.error(e);
            return new ErrorModel("获取用户信息失败，请联系管理员！");
        }

        return new SuccessModel(userList);
    }

    private String check(AddUserReq addUserReq) {

        if(StringUtils.isBlank(addUserReq.getName()))
        {
            return "请输入姓名";
        }
        if(StringUtils.isBlank(addUserReq.getUserName()))
        {
            return "请输入用户名";
        }
        if(StringUtils.isBlank(addUserReq.getPassWord()))
        {
            return "请输入密码";
        }
        return null;
    }

    private User convertUser(AddUserReq addUserReq) {
        User user = new User();
        user.setUserName(addUserReq.getName());
        return user;
    }

    private UserLogin convertUserLogin(AddUserReq addUserReq) {
        UserLogin userLogin = new UserLogin();
        userLogin.setUserName(addUserReq.getUserName());
        userLogin.setPassword(addUserReq.getPassWord());
        return userLogin;
    }
}
